package com.glsx.auto;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

import static com.glsx.auto.DefaultPackage.*;

/**
 * Created by linxy on 2017/4/23.
 */
public class ProjectModule {

    private static final String MAIN = File.pathSeparatorChar + "src/main";
    private static final String SRC_ROOT = MAIN + File.pathSeparatorChar + "java";
    private static final String RESOURCE = File.pathSeparatorChar + "resource";

    private static final String WEB_APP = File.pathSeparatorChar + "webapp";
    private static final String WEB_INF = File.pathSeparatorChar + "WEB-INF";
    private static final String BIN = File.pathSeparatorChar + "bin";

    private static final String CONFIG = File.pathSeparatorChar + "config";
    private static final String ENTITY = File.pathSeparatorChar + "entity";

    private String parentDirectory;
    private String moduleName;
    private String topLevelPackageName;

    public ProjectModule(String parentDirectory, String moduleName, String topLevelPackageName) {
        this.parentDirectory = parentDirectory;
        this.moduleName = moduleName;
        this.topLevelPackageName = topLevelPackageName;
    }

    public ProjectModule(String parentDirectory, String moduleName) {
        this(parentDirectory, moduleName, "");
    }

    public String getParentDirectory() {
        return parentDirectory;
    }

    public String getModuleName() {
        return moduleName;
    }

    public String getTopLevelPackageName() {
        return topLevelPackageName;
    }

    public String getModuleDirectory() {
        return getParentDirectory() + File.pathSeparatorChar + getModuleName();
    }

    public String getMainDirectory() {
        return getModuleDirectory() + MAIN;
    }

    public String getSrcRootDirectory() {
        return getModuleDirectory() + SRC_ROOT;
    }

    public String getTopPackageDirectory() {
        if(!StringUtils.isEmpty(getTopLevelPackageName())) {
            return getSrcRootDirectory() + File.pathSeparatorChar + getTopLevelPackageName().replace('.', File.pathSeparatorChar);
        }
        return getSrcRootDirectory();
    }

    public String getPackageName(String packageName) {
        if(!StringUtils.isEmpty(getTopLevelPackageName())) {
            return getTopLevelPackageName() + "." + packageName;
        }
        return packageName;
    }

    public String getPackageName(DefaultPackage defaultPackage) {
        return getPackageName(defaultPackage.getName());
    }

    public String getPackageDirectory(String packageName) {
        return getTopPackageDirectory() + File.pathSeparatorChar + packageName.replace('.', File.pathSeparatorChar);
    }

    public String getPackageDirectory(DefaultPackage packageName) {
        return getPackageDirectory(packageName.getName());
    }

    public String getResourceDirectory() {
        return getMainDirectory() + RESOURCE;
    }

    public String getResourceConfigDirectory() {
        return getResourceDirectory() + CONFIG;
    }

    public String getResourceEntityHbmMapperDirectroy() {
        return getResourceDirectory() + ENTITY;
    }

    public String getWebappDirectory() {
        return getMainDirectory() + WEB_APP;
    }

    public String getWebInfDirectory() {
        return getWebappDirectory() + WEB_INF;
    }

    public String getBinDirectory() {
        return getWebInfDirectory() + BIN;
    }

    public boolean createFrameWorkDirectories(boolean container) {
        boolean ok =  Utils.createDirectories(getTopPackageDirectory());
        if(container) {
            ok = ok && Utils.createDirectories(getResourceConfigDirectory()) &&
                    Utils.createDirectories(getResourceEntityHbmMapperDirectroy()) &&
                    Utils.createDirectories(getBinDirectory()) &&
                    Utils.createDirectories(getPackageDirectory(CONTAINER_BASE.getName())) &&
                    Utils.createDirectories(getPackageDirectory(CONTAINER_DAO.getName())) &&
                    Utils.createDirectories(getPackageDirectory(CONTAINER_SERVICE_IMPL.getName()));
        } else {
            ok = ok && Utils.createDirectories(getPackageDirectory(COMMON_BASE.getName())) &&
                    Utils.createDirectories(getPackageDirectory(COMMON_ENTITY.getName())) &&
                    Utils.createDirectories(getPackageDirectory(COMMON_SERVICE.getName()));
        }
        return ok;
    }

}
